package com.liu.controller;

import com.liu.domain.*;
import com.liu.repository.JiFenRepository;
import com.liu.repository.VoteLimitRepository;
import com.liu.repository.VotecountRepository;
import com.liu.utils.TimeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * Created by liu on 17-10-31.
 */
@RestController
public class VotecountController {

    private final static Logger logger = LoggerFactory.getLogger(VotecountController.class);

    @Autowired
    private VotecountRepository VotecountRepository;
    @Autowired
    private VoteLimitRepository voteLimitRepository;

    @Autowired
    private JiFenRepository jiFenRepository;

    /*
    查询所有
     */
    @GetMapping(value = "/VC")
    public List<VoteCount> textList() {
        List<VoteCount> counts = VotecountRepository.findAll();
        for ( VoteCount r: counts) {
            int Count = r.getRoomId();
            String r_id = String.valueOf(Count);
            String buildId = r_id.substring(0, 2);
            r_id = r_id.substring(2, 5);
            r.setFang(buildId + "#" + r_id);
        }
        return counts;
    }


   // 查询有几个投票count

    @GetMapping(value = "/VCC")
    public int textList4() {
        int voteCount = VotecountRepository.findCount();
        return voteCount;
    }

/*
    查询所有从大到小
     */

    @GetMapping(value = "/VCS")
    public List<VoteCount> text3() {
//        System.out.println("textList");
//
//        logger.info("textList");

        List<VoteCount> VoteCounts = VotecountRepository.findShunxun();
        for ( VoteCount r: VoteCounts) {
            int Count = r.getRoomId();
            String r_id = String.valueOf(Count);
            String buildId = r_id.substring(0, 2);
            r_id = r_id.substring(2, 5);
            r.setFang(buildId + "#" + r_id);
        }
        return VoteCounts;

    }
    /*
    报名投票 不满足条件不能报名
     */
    @PostMapping(value = "/VC")
    public ModelAndView VCAdd(VoteCount vc) {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("student/stuVote");
        System.out.println(vc.getRoomId());
        int[] time = TimeUtil.dealDateToSchool(new Date());
        System.out.println(time[0]+""+time[1]);
        JiFen jifen = jiFenRepository.findByRoomIdAndYearsAndTerms(vc.getRoomId(), time[0], time[1]);
        if(jifen.getScore()>=110){
            VotecountRepository.save(vc);
            mav.addObject("flag", 1);
        }else {
            mav.addObject("flag", 0);
        }
        return mav;
    }

    /*
    更新一个的信息
     */
    @PutMapping(value = "/VC/{id}")
    public VoteCount studentUpdate(@PathVariable("id") int id, VoteCount VC) {

        return VotecountRepository.save(VC);
    }
    /*
    删除一个
     */

    @DeleteMapping(value = "/VC/{id}")
    public void studentDelete(@PathVariable("id") int id) {
        VotecountRepository.delete(id);
    }

    @PostMapping("/upload")
    public boolean s() {
        return true;
    }

//投票计数器加一
    @GetMapping(value = "/addVC/{id}")
    public boolean addCount(@PathVariable("id") int id, HttpSession session){
        student student = (com.liu.domain.student) session.getAttribute("user");
        boolean ok = false;
        if (student != null) {
            String stuId = student.getStu_id();
            int num = VotecountRepository.updateCount(id);
            if (num > 0) {
                ok = true;
            }
            if (ok) {
                VoteCount voteCount = VotecountRepository.getOne(id);
                int topicId = voteCount.getTopicId();
                VoteLimit voteLimit = new VoteLimit();
                voteLimit.setStuId(stuId);
                voteLimit.setTopicId(topicId);
                voteLimitRepository.save(voteLimit);
            }
        }
        return ok;
    }

    @GetMapping("/testVote/{id}")
    public boolean testVote(@PathVariable("id") int id, HttpSession session) {
        student student = (com.liu.domain.student) session.getAttribute("user");
        boolean ok = true;
        if (student != null) {
            String stuId = student.getStu_id();
            VoteCount voteCount = VotecountRepository.getOne(id);
            int topicId = voteCount.getTopicId();
            VoteLimit voteLimit = voteLimitRepository.isVoted(stuId, topicId);
            if (voteLimit != null) {
                ok = false;
            }
        }
        return ok;
    }


   /* 删除限制表 所有数据*/
    @GetMapping(value = "/VCDL")
    public void delall() {
        voteLimitRepository.del();
    }


    /* 删除count表 所有数据*/
    @GetMapping(value = "/VCDC")
    public void delcallC() {
        VotecountRepository.del();
    }


    //判断有没有学生投票
    @GetMapping("/HVC")
    public boolean isVoted(HttpSession session) {
        boolean ok = false;
        student stu = (student) session.getAttribute("user");
        int count = VotecountRepository.findbyroom(stu.getRoom_id());
        if (count > 0) {
            ok = true;
        }
        return ok;
    }

}
